package com.ruoyi.common.utils.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Calendar;

/**
 * @author tanjianjun
 * @version 1.o
 * @description OssServiceImpl
 * @since 2021/4/12 17:07
 */
@Service
public class OssServiceImpl implements OssService {
    @Override
    public String uploadFile(MultipartFile file) throws Exception {
        //读取工具类的数据
        String accessKeyId = "LTAI5tS9Bj1DdwaRu4Kt7Kbf";
        String accessKeySecret = "RbGLd0I1ei8Zx9wiBhvzvwROGFywgZ";
        String bucketName = "wukongsudi-01";
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
        //连接oss客户端
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 上传文件流
        InputStream inputStream = file.getInputStream();
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int date = c.get(Calendar.DATE);
        //自己看情况
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        //根据时间拼接url
        String url = year+"/"+month+"-"+date+"/"+hour+"-"+minute+"-"+second+"/"+file.getOriginalFilename();
        //上传
        ossClient.putObject(bucketName, url, inputStream);
        // 关闭OSSClient。
        ossClient.shutdown();
        return "https://"+bucketName+"."+endpoint+"/"+url;

    }
}
